<?php
namespace CIDB;
/**
 * CodeIgniter
 *
 * An open source application development framework for PHP
 *
 * This content is released under the MIT License (MIT)
 *
 * Copyright (c) 2014 - 2016, British Columbia Institute of Technology
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 * @package	CodeIgniter
 * @author	EllisLab Dev Team
 * @copyright	Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
 * @copyright	Copyright (c) 2014 - 2016, British Columbia Institute of Technology (http://bcit.ca/)
 * @license	http://opensource.org/licenses/MIT	MIT License
 * @link	https://codeigniter.com
 * @since	Version 1.0.0
 * @filesource
 */

function show_error($message = '') {
	echo $message;
}

function log_message($l, $m) {
	echo '[' . $l . ']:' . $m;
}

function &DB($db_type = 'master', $query_builder_override = NULL){
	// Load the DB config file if a DSN string wasn't passed
	if ($db_type != 'master' && $db_type != 'slave'){
		show_error('The 1st param of function DB must eq "master" or "slave"');
	}
	
	$params = Config::db[$db_type];
	
	// Load the DB classes. Note: Since the query builder class is optional
	// we need to dynamically create a class that extends proper parent class
	// based on whether we're using the query builder class or not.
	if ($query_builder_override !== NULL){
		$query_builder = $query_builder_override;
	}
	// Backwards compatibility work-around for keeping the
	// $active_record config variable working. Should be
	// removed in v3.1
	elseif ( ! isset($query_builder) && isset($active_record)){
		$query_builder = $active_record;
	}
	
	
	if ( ! isset($query_builder) OR $query_builder === TRUE)
	{
		if ( ! class_exists('CI_DB', FALSE))
		{
			/**
			 * CI_DB
			 *
			 * Acts as an alias for both CI_DB_driver and CI_DB_query_builder.
			 *
			 * @see	CI_DB_query_builder
			 * @see	CI_DB_driver
			 */
			class CI_DB extends CI_DB_query_builder { }
		}
	}
	elseif ( ! class_exists('CI_DB', FALSE))
	{
		/**
		 * @ignore
		 */
		class CI_DB extends CI_DB_driver { }
	}
	
	// Instantiate the DB adapter
	$driver = 'CIDB\drivers\mysqli\CI_DB_'.$params['dbdriver'].'_driver';
	$DB = new $driver($params);
	
	$DB->initialize();
	return $DB;
}

/**
 * 创建数据库连接
 * 
 * 使用方式： 
 * 			$db = new DB('master')->create();
 *          $db->query('select 1')->row_array();
 * 
 * @author Yuan Xibin
 *
 */
class DB{
	public $db = null;
	private $_type = '';
	private $_query_builder_override = '';
	
	public function __construct($type='master', $query_builder_override = NULL){
		$this->_type = $type;
		$this->_query_builder_override = $query_builder_override;
	}
	
	public function create(){
		return DB($this->_type, $this->_query_builder_override);
	}
}